package com.woniuxy.woniuhis.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.woniuxy.woniuhis.entity.SurgeryBooking;
import com.woniuxy.woniuhis.entity.SurgeryRoom;
import com.woniuxy.woniuhis.service.SurgeryBookingService;
import com.woniuxy.woniuhis.service.SurgeryRoomService;
import com.woniuxy.woniuhis.util.ResponseInfo;
import com.woniuxy.woniuhis.util.ResponseJson;
import com.woniuxy.woniuhis.util.UUIDUtil;

@Controller
@RequestMapping("/surgeryroom")
public class SurgeryRoomController {

	@Autowired
	SurgeryRoomService surgeryRoomService;

	@Autowired
	SurgeryBookingService surgeryBookingService;

	/**
	 * 添加手术室
	 */
	@RequestMapping("/addsurgerysoom")
	@ResponseBody
	public ResponseJson addSurgeryRoom(SurgeryRoom surgeryRoom ) {
		ResponseJson responseJson = new ResponseJson();
		try {
			// 数据验证
			if(surgeryRoom.getSurgeryRoomName()==null || surgeryRoom.getSurgeryRoomName().equals("")) {
				throw new RuntimeException("手术室名称不能为空");
			}
			surgeryRoom.setSurgeryRoomId(UUIDUtil.uuid32());
			surgeryRoomService.addSurgeryRoom(surgeryRoom);
			responseJson.setMsg("添加成功");
			return responseJson;
		} catch (Exception e) {
			responseJson.setCode(500);
			responseJson.setMsg(e.toString());
			e.printStackTrace();
			return responseJson;
		}
	}

	/**
	 * 更新手术室
	 */
	@RequestMapping("/editsurgerysoom")
	@ResponseBody
	public ResponseJson modifySurgeryRoom(SurgeryRoom surgeryRoom) {
		ResponseJson responseJson = new ResponseJson();
		try {
			// 数据验证
			SurgeryRoom surRoom = surgeryRoomService.findSurgeryRoomById(surgeryRoom.getSurgeryRoomId());
			if (surRoom == null) {
				throw new RuntimeException("手术室id不正确");
			}
			surgeryRoomService.modifySurgeryRoom(surgeryRoom);
			responseJson.setMsg("修改成功");
			return responseJson;
		} catch (Exception e) {
			responseJson.setCode(500);
			responseJson.setMsg(e.toString());
			e.printStackTrace();
			return responseJson;
		}
	}

	/**
	 * 查询指定手术室
	 */
	@RequestMapping("/roominfo")
	@ResponseBody
	public ResponseInfo findSurgeryRoomById(String surgeryRoomId) {
		ResponseInfo responseInfo = new ResponseInfo();
		try {
			SurgeryRoom surgeryRoom = surgeryRoomService.findSurgeryRoomById(surgeryRoomId);
			if (surgeryRoom == null) {
				throw new RuntimeException("手术室id不正确");
			}
			responseInfo.setData(surgeryRoom);
			responseInfo.setState(200);
			return responseInfo;
		} catch (Exception e) {
			responseInfo.setState(500);
			responseInfo.setMsg(e.toString());
			e.printStackTrace();
			return responseInfo;
		}
	}
	
	/**
	 *	获取全部的手术室 	
	 */
	@RequestMapping("/rooms")
	@ResponseBody
	public ResponseInfo getAllSurgeryRooms() {
		ResponseInfo responseInfo = new ResponseInfo();
		try {
			SurgeryRoom surgeryRoom= new SurgeryRoom();
			List<SurgeryRoom> allSurgeryRooms = surgeryRoomService.findAllSurgeryRooms(surgeryRoom);
			responseInfo.setData(allSurgeryRooms);
			responseInfo.setState(200);
			return responseInfo;
		} catch (Exception e) {
			responseInfo.setState(500);
			responseInfo.setMsg(e.toString());
			e.printStackTrace();
			return responseInfo;
		}
	}

	/**
	 * 查询符合条件的手术室，可以不要条件，分页查询 手术室和手术室的预约信息
	 */
	@RequestMapping("/roomsandbooking")
	@ResponseBody
	public ResponseInfo getAllSurgeryRoomsAndBooking(SurgeryRoom surgeryRoom, Integer pageIndex, Integer pageSize) {
		ResponseInfo responseInfo = new ResponseInfo();
		try {
			// 判断surgeryRoom对象属性值是否合法
			if (surgeryRoom == null) {
				surgeryRoom = new SurgeryRoom();
				System.out.println("surgeryRoom为null");
			}
			if (pageIndex == null) {
				pageIndex = 1;
			}
			if (pageSize == null) {
				pageSize = 4;
			}
			System.out.println(surgeryRoom + ":" + pageIndex + ":" + pageSize);
			PageInfo<SurgeryRoom> surgeryRooms = surgeryRoomService.findSurgeryRooms(surgeryRoom, pageIndex, pageSize);
			for (SurgeryRoom surRoom : surgeryRooms.getList()) {
				SurgeryBooking sb = new SurgeryBooking();
				sb.setSurgeryRoomId(surRoom.getSurgeryRoomId());
				PageInfo<SurgeryBooking> surgeryBookings = surgeryBookingService.findSurgeryBookings(sb, pageIndex,
						3);
//				for(SurgeryBooking sbing:surgeryBookings.getList()) {
//					System.out.println(sbing);
//				}
				surRoom.setSurgeryBookings(surgeryBookings);
			}

			responseInfo.setData(surgeryRooms);
			responseInfo.setState(200);
			return responseInfo;
		} catch (Exception e) {
			responseInfo.setState(500);
			responseInfo.setMsg(e.toString());
			e.printStackTrace();
			return responseInfo;
		}
	}

	/**
	 * 指定手术室的信息和预约信息
	 * 
	 * @param surgeryRoomId
	 * @param pageIndex
	 * @param pageSize
	 * @return
	 */
	@RequestMapping("/room")
	@ResponseBody
	public ResponseInfo getAllSurgeryRoomById(String surgeryRoomId, Integer pageIndex, Integer pageSize) {
		ResponseInfo responseInfo = new ResponseInfo();
		System.out.println("surgeryRoomId:" + surgeryRoomId + ",pageIndex:" + pageIndex + ",pageSize:" + pageSize);
		try {
			if (surgeryRoomId == null || surgeryRoomId == "") {
				throw new RuntimeException("手术室id为空");
			}
			SurgeryRoom surgeryRoom = surgeryRoomService.findSurgeryRoomById(surgeryRoomId);
			if (surgeryRoom == null) {
				throw new RuntimeException("手术室id不正确");
			}
			if (pageIndex == null) {
				pageIndex = 1;
			}
			if (pageSize == null) {
				pageSize = 10;
			}
			SurgeryBooking sb = new SurgeryBooking();
			sb.setSurgeryRoomId(surgeryRoom.getSurgeryRoomId());
			PageInfo<SurgeryBooking> surgeryBookings = surgeryBookingService.findSurgeryBookings(sb, pageIndex,
					pageSize);
			surgeryRoom.setSurgeryBookings(surgeryBookings);
			responseInfo.setData(surgeryRoom);
			responseInfo.setState(200);
			return responseInfo;
		} catch (Exception e) {
			responseInfo.setState(500);
			responseInfo.setMsg(e.toString());
			e.printStackTrace();
			return responseInfo;
		}
	}
}
